import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;

public class GUI extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5730381841456945746L;
	ImagePanel content;
	JPanel actions;
	JPanel main;
	JTextArea out;
	JTextArea in;
	JButton enter;

	public GUI() {

		content = new ImagePanel("ca.png");
		actions = new JPanel();
		main = new JPanel();
		
		out = new JTextArea();
		in = new JTextArea();
		
		JScrollPane outPane = new JScrollPane(out);
		JScrollPane inPane = new JScrollPane(in);
		
		enter = new JButton("enter");

		main.setLayout(null);
		
		out.setBorder(BorderFactory.createLineBorder(Color.black));
		in.setBorder(BorderFactory.createLineBorder(Color.black));

		inPane.setBorder(BorderFactory.createLineBorder(Color.black));
		outPane.setBorder(BorderFactory.createLineBorder(Color.black));
		
		inPane.setSize(300, 200);
		
		setSize(700, 500);
		actions.setBounds(500, 0, 200, 500);
		content.setBounds(0, 0, 500, 500);
		actions.setLayout(new GridLayout(2, 2));

		actions.add(inPane);
		actions.add(outPane);

		actions.add(enter);
		main.add(content, BorderLayout.CENTER);
		main.add(actions, BorderLayout.EAST);
		setContentPane(main);

		enter.addActionListener(this);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		int BA = 0;
		int lineCount = 1;
		Map<Integer, String> instruction = new HashMap<Integer, String>();
		int startAddress = -1;
		// Get paragraph element
		Element paragraph = in.getDocument().getDefaultRootElement();

		// Get number of content elements
		int contentCount = paragraph.getElementCount();

		// Get index ranges for each content element.
		// Each content element represents one line.
		// Each line includes the terminating newline.
		for (int i = 0; i < contentCount; i++) {
			Element s = paragraph.getElement(i);
			int rangeStart = s.getStartOffset();
			int rangeEnd = s.getEndOffset();
			try {

				String line = in.getText(rangeStart, rangeEnd - rangeStart);
				System.out.print(line);

				StringTokenizer st = new StringTokenizer(line);

				if (st.nextToken().equalsIgnoreCase("BaseAddress")) {
					BA = Integer.parseInt(st.nextToken());
					if (BA < 0) {
						JOptionPane.showMessageDialog(this,
								"Base Address can't be zero.");
						return;
					}
					if (startAddress == -1) {
						startAddress = BA;
					}
				} else {
					instruction.put((BA + ((lineCount - 1) * 4)), line);
				}
				lineCount++;
			} catch (BadLocationException ex) {
				System.out.println(ex);
			}

		}
		try {
			Bus.setInitialValues(instruction, startAddress == -1 ? 0
					: startAddress);
			Bus.startSequence(out);
		} catch (Exception e2) {
			JOptionPane.showMessageDialog(this, "Exception: " + e2
					+ "\nInvalid Syntax.");
			e2.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new GUI();

	}

	public StringTokenizer instructionRead(String instruction) {
		StringTokenizer x = new StringTokenizer(instruction);
		return x;

	}

}
